/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-09-15 14:14:23
 * @filename : static/logic/nmm/src/views/upgrade.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
    var paypalView, userInfo, 
        mId = 'payment',reRender = false,menuName = 'payment',
        panel = require('{component}panel/zepto.panel'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        tpl = require('{view}payment.tpl'),
        tplN = require('{view}userNav.tpl');

    paypalView = Backbone.View.extend({

        el: '',

        template: _.template(tpl),
        templateN: _.template(tplN),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId); 
            var month = parseInt(obj.num),arr=[1,3,6];
            if (arr.indexOf(month) < 0) {
                window.location.href = '#upgrade/whole'
                return;
            }

            this.month = month;

            if ($('#'+mId).length < 1) { 
                $('body').append(this.template(lang));
                this.bindEvt();
                userInfo = share.userInfo();
            } else {
                $('#'+mId).css('display','block');
            }

            share.panel(panel,this.templateN,mId,menuName);
            this.paymentInfo(month);
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);
            dom.find('.back').on('tap',function(e){
                window.location.href='#upgrade/whole'; 
            });

            dom.find('.paypalWay').on('tap',function(){
                var ajax = $(this).attr('ajax');
                if (ajax != 1) {
                    tthis.handlePaypal(this); 
                }
            });
        },

        paymentInfo : function(month) {
            var dom = $('#'+mId),
                userName = userInfo.username, 
                monthPay = this.getMonthPay(month),
                //payMoney = parseFloat((monthPay * month) /100).toFixed(2);
                payMoney = this.getPayMoney(month);

            dom.find('.userName').html(userName); 
            dom.find('.monthNum').html(month); 
            dom.find('.monthPay').html(monthPay/100); 
            dom.find('.payMoney').html(payMoney); 
        },

        getPayMoney : function(month) {
            var num;
            if(month == 6) {
                num = 95.95;
            } else if(month == 3) {
                num = 59.95;
            } else if(month == 1) {
                num = 29.95;
            }
            return num;
        },

        getMonthPay : function(month) {
            //JS float calculation error
            var num;
            if(month == 6) {
                num = 1595;
            } else if(month == 3) {
                num = 1995;
            } else if(month == 1) {
                num = 2995;
            }
            return num;
        },

        handlePaypal: function(that) {
            var dom = $(that), tthis = this;
            dom.attr('ajax',1);
            share.ajaxStartA(dom);
            var obj = {
                url : seajs.data.vars.apiAccessUrl + 'paypal',
                type : 'POST',
                data : { months: tthis.month, original_url: share.getStorage("cur_url") },
                success : function(data) {
                    if (data.ret == 1) {
                        window.location.href = data.url;
                    } else {
                        console.log(data.msg);
                    }
                },
                error : function(xhr, errorType, error) {
                },
                complete : function(xhr, status) {
                    //dom.attr('ajax',0);
                    //share.ajaxEndA(dom);
                }
            };
            $.ajax(share.ajaxControl(obj));
        }
    });
    module.exports = paypalView;
});
